package org.itboys.core.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.entity.mongo.SundayCouponOrderSubstract;
import org.itboys.core.entity.mongo.SundayCouponSubstract;
import org.itboys.core.entity.mysql.SundayCoupon;
import org.itboys.core.entity.mysql.SundayCouponOrder;
import org.itboys.core.service.mysql.web.SundayCouponOrderService;
import org.itboys.core.service.mysql.web.SundayCouponService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author：贾琦磊
 * @Date：2018/8/30
 * @Description：促销单
 */
@Controller
@RequestMapping("/sunday/web/coupon/order")
public class SundayCouponOrderController extends BaseController{


    @Autowired
    private SundayCouponOrderService couponOrderService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response){
        return "sunday/coupon/order/index";
    }
    @RequestMapping("/input")
    public String input(HttpServletRequest request,HttpServletResponse response
            ,Model model
            ,@RequestParam("id")Long id){
        SundayCouponOrder couponOrder = couponOrderService.findOne(id);
        model.addAttribute("couponOrder",couponOrder);
        return "/sunday/coupon/order/input";
    }

    @RequestMapping(value = "/save")
    public void save(HttpServletRequest request, HttpServletResponse response
            , @RequestParam(value = "id")Long id
            , @RequestParam(value = "startDate")String startDate
            ,@RequestParam(value = "endDate")String endDate
            ,@RequestParam(value = "couponWay")Integer couponWay
            ,@RequestParam(value = "saleArea")Integer saleArea
            ,@RequestParam(value = "userArea")List<Integer> userArea
            , @RequestParam(value = "skuId")List<Long> skuIds
            , @RequestParam(value = "baseAmount") String baseAmount
            ,@RequestParam(value = "subAmount") String subAmount)throws Exception{
        SundayCouponOrderSubstract couponOrderSubstract=new SundayCouponOrderSubstract(baseAmount,subAmount);
        SundayCouponOrder couponOrder=new SundayCouponOrder(startDate,endDate, couponWay,  saleArea, SundayCommonConstants.COUPON_STATE_USE);
        String msg="";
        if(!ListUtil.isNotNull(skuIds)){
            ResponseMessage.error(CommonConstants.FAIL_CODE,"请添加sku");
        }else if(!ListUtil.isNotNull(userArea)){
            ResponseMessage.error(CommonConstants.FAIL_CODE,"请选择用户范围");
        }
        couponOrderService.save(id,couponOrder,couponOrderSubstract,userArea,skuIds);
        ResponseMessage.success();
    }

    @RequestMapping(value = "/select")
    public void select(HttpServletRequest request,HttpServletResponse response){
        PageResult<SundayCouponOrder> result = couponOrderService.select(request);
        JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
    }

    @RequestMapping(value = "/delete")
    public void delete(HttpServletRequest request,HttpServletResponse response,
                       @RequestParam(value = "id")Long id){
        couponOrderService.delete(id);
        ResponseMessage.success();
    }

}
